package org.example.dao.test;

import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.JDBCConnectionFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * @author: baize(binhua.wu @ hipac.cn)
 * @date: 2019/1/10 19:22
 * @desc:
 */
public class LombokPlugin extends PluginAdapter {

    public boolean validate(List<String> list) {
        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        topLevelClass.addImportedType("lombok.Data");
        //topLevelClass.addImportedType("lombok.Builder");
        //自己按需加吧
        //topLevelClass.addImportedType("lombok.NoArgsConstructor");
        //topLevelClass.addImportedType("lombok.AllArgsConstructor");

        topLevelClass.addAnnotation("@Data");
        //topLevelClass.addAnnotation("@Builder");
        //topLevelClass.addAnnotation("@NoArgsConstructor");
        //topLevelClass.addAnnotation("@AllArgsConstructor");

        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine("* " + getTableComment(introspectedTable.getFullyQualifiedTable()));
        topLevelClass.addJavaDocLine("*/");

        return true;
    }

    private String getTableComment(FullyQualifiedTable table) {
        String tableComment = "";
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            JDBCConnectionFactory jdbc = new JDBCConnectionFactory(context.getJdbcConnectionConfiguration());
            connection = jdbc.getConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery("SHOW CREATE TABLE " + table.getIntrospectedTableName());

            if (rs != null && rs.next()) {
                String createDDL = rs.getString(2);
                int index = createDDL.indexOf("COMMENT='");
                if (index < 0) {
                    tableComment = "";
                } else {
                    tableComment = createDDL.substring(index + 9);
                    tableComment = tableComment.substring(0, tableComment.length() - 1);
                }
            }

        } catch (SQLException e) {

        } finally {
            closeConnection(connection, statement, rs);
        }
        return tableComment;
    }

    private void closeConnection(Connection connection, Statement statement, ResultSet rs) {
        try {
            if (null != rs)
                rs.close();
        } catch (SQLException e) {

            e.printStackTrace();
        } finally {
            try {
                if (statement != null)
                    statement.close();
            } catch (Exception e) {
                e.printStackTrace();

            } finally {

                try {
                    if (connection != null)
                        connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass,
                                              IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return false;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass,
                                              IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return false;
    }
}
